require('es6-promise').polyfill();

import axios from 'axios'

function loadDeliverys(data) {
  return {
    type: 'LOAD_DELIVERY_DATA',
    data
  }
}

function createDelivery(data, response) {
  return {
    type: 'CREATE_DELIVERY_DATA',
    data,
    response
  }
}

function errorAction(error) {
  return {
    type: 'ERROR_DELIVERY',
    error
  }
}

export function apiMsgAction(msg) {
  return {
    type: 'API_MSG',
    msg
  }
}

export function createDeliveryAsync(data) {
  return function(dispatch) {
    return axios.post('/api/delivery/create', data).then(
      resp => dispatch(loadDeliverysAsync({})),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}


export function loadDeliverysAsync(data) {
  return function(dispatch) {
    return axios.post('/api/delivery', data).then(
      resp => dispatch(loadDeliverys(resp.data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}
